package com.daohang361.imqq.pugin.service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.daohang361.imqq.Asycore.MyLoger;
import com.daohang361.imqq.service.InterfaceService;
import com.daohang361.imqq.tool.Hex2Bytes;

/** 
 * @author naner E-mail:likun3344@163.com 
 * @create time：2011-6-22 下午09:48:02 
 */
public class GetFriendheadHttpClient implements InterfaceService{

	@Override
	public Object doService(Object... o) {
		try {
			System.out.println("GetFriendheadHttpClient.......................");
			doGet(o);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Object invoke(Object... o) {
		// TODO Auto-generated method stub
		return null;
	}

	public void doGet(Object... o) throws IOException{
		int mainQQ=Integer.parseInt(o[0]+"");
		int friendQQ=Integer.parseInt(o[1]+"");
		String X_FILEHASH=o[2]+"";
		String X_SIGNSTRING=o[3]+"";
		String filePath=o[4]+"";
	       URL loginUrl = new URL("http://183.60.13.208/GetFile");
	       HttpURLConnection connection = (HttpURLConnection) loginUrl.openConnection();
	       connection.setRequestMethod("GET");
	       connection.setRequestProperty("Accept", "*/*");
	       connection.setRequestProperty("X-VERSION", "1");
	       connection.setRequestProperty("X-SRCUSERTYPE", "1");
	       connection.setRequestProperty("X-SRCUIN",mainQQ+"");
	       connection.setRequestProperty("X-DSTUIN", friendQQ+"");
	       connection.setRequestProperty("X-DSTUSERTYPE", "1");
	       connection.setRequestProperty("X-IMAGETYPE", "1");
	       connection.setRequestProperty("X-SIGNSTRING",X_SIGNSTRING) ;
	       connection.setRequestProperty("X-FILEHASH",X_FILEHASH);
	       connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
	       connection.setRequestProperty("Pragma", "no-cache");
	       connection.connect();
	       ByteArrayOutputStream baos = new ByteArrayOutputStream();
	       InputStream ins =  connection.getInputStream();
	       byte[] temp=new byte[1024];
	       int len=0;
	       while((len=ins.read(temp,0,temp.length))!=-1)
	    	{
	    	   baos.write(temp, 0, len);
	    	}
	       byte[] buffer =  baos.toByteArray();
	       connection.disconnect();
		   OutputStream ot = new FileOutputStream(new File(filePath));
		   ot.write(buffer);
		   ot.close();
	}
}
